<template>
  <el-dialog width="400" :title="dialogTitle" v-model="dialogVisible" modal draggable @close="closeDialog"
    :close-on-click-modal="false">
    <el-form class="dialog-form" :model="formData" ref="vForm" label-position="right" label-width="80px" size="medium"
      @submit.native.prevent :rules="formRules" :disabled="formDisabled">
      <el-form-item label="读写器名称" prop="deviceName">
        <el-input v-model="formData.deviceName" placeholder="请输入读写器名称" link clearable></el-input>
      </el-form-item>
      <el-form-item label="读写器IP" prop="deviceIp">
        <el-input v-model="formData.deviceIp" placeholder="请输入读写器IP" link clearable></el-input>
      </el-form-item>
      <el-form-item label="读写器端口" prop="devicePort">
        <el-input v-model="formData.devicePort" placeholder="请输入读写器端口" link clearable></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="formData.note" placeholder="请输入备注" :rows="2" type="textarea" clearable></el-input>
      </el-form-item>
    </el-form>
    <div v-if="!formDisabled" slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
      <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, update, getInfo } from '@/api/bas/rfidPrint'
export default {
  name: 'readerDialog',
  data() {
    return {
      type: '',
      dialogTitle: '',
      dialogVisible: false,
      formData: {},
      formDisabled: false,
      formRules: {
        deviceName: [{ required: true, message: '请输入读写器名称', trigger: 'blur' }],
        deviceIp: [{ required: true, message: '请输入读写器IP', trigger: 'blur' }],
        devicePort: [{ required: true, message: '请输入读写器端口', trigger: 'change' }],
      },
    }
  },
  methods: {
    add() {
      this.formDisabled = false
      this.type = 'add'
      this.dialogTitle = '添加RFID读写器'
      // 先清空表单数据
      this.formData = {}
      this.dialogVisible = true
    },
    edit(row) {
      this.formDisabled = false
      this.type = 'edit'
      this.dialogTitle = '编辑角色'
      getInfo(row.roleId)
        .then((res) => {
          if (res.data.code === 200) {
            this.formData = res.data.data
            this.dialogVisible = true
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    show(row) {
      this.formDisabled = true
      this.type = 'show'
      this.dialogTitle = '查看角色'
      getInfo(row.roleId)
        .then((res) => {
          if (res.data.code === 200) {
            this.formData = res.data.data
            this.dialogVisible = true
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    submitForm() {
      this.$refs['vForm'].validate((valid) => {
        if (valid) {
          if (this.type === 'add') {
            add(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('添加成功')
                  this.closeDialog()
                  this.$emit('ok')
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          } else {
            update(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('修改成功')
                  this.closeDialog()
                  this.$emit('ok')
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          }
        }
      })
    },
    closeDialog() {
      this.$refs['vForm'].resetFields()
      this.dialogVisible = false
    },
  },
}
</script>

<style scoped>
.dialog-form {
  padding: 0 30px;
  /* 根据需求调整数值 */
}
</style>
